package com.xiaoyao.myframeapp.mvp.base;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.xiaoyao.myframeapp.FrameApplication;
import com.xiaoyao.myframeapp.mvp.IView;
import com.xiaoyao.myframeapp.utils.L;
import com.xiaoyao.myframeapp.utils.SpUtils;
import com.xiaoyao.myframeapp.utils.db.XUtilDb;

import org.xutils.DbManager;
import org.xutils.x;

import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


/**
 * @author xiaoyao
 * @mail xiao80206040@qq.com
 * @explain BaseActivity
 * @Created by Administrator on 2017/2/27 0027.
 */
public abstract class BaseActivity<P extends BasePresenter> extends RxAppCompatActivity implements
//public abstract class BaseActivity<P extends BasePresenter> extends FragmentActivity implements
        IView, View.OnClickListener {
    protected P mPresenter;
    protected View view;
    protected DbManager db;

    {
        try {
            db = x.getDb(XUtilDb.getDaoConfig());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 线程调度
     */
    public <T> ObservableTransformer<T,T> setThread(){
        return new ObservableTransformer<T,T>() {
            @Override
            public ObservableSource<T>  apply(Observable<T>  upstream) {
                return upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    /**
     * Activity 重新加载方法
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        L.i(getClass(), "onRestart");
        initView();
        initListener();
        initData();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
        //TODO 百度地图 每次使用 初始化
//        SDKInitializer.initialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(getView());
        ButterKnife.bind(this);
        mPresenter = loadPresenter();

        //TODO 在创建时添加  销毁时清除
        FrameApplication.getActivitys().add(this);

        initCommonData();
        initView();
        initListener();
        initData();
    }

    //TODO 全部关闭打开 自定义界面Activity


    //TODO 返回首页 HomeActivity
    public static void Home_Activity(Context context) {
        if (Integer.valueOf(SpUtils.get("id", 0).toString()) > 0) {
            for (Activity activity : FrameApplication.getActivitys()) {
                String nameaaa = activity.getClass().getName();
                if (nameaaa.contains("HomeActivity")) {
                    continue;
                }
                activity.finish();
            }
        }
    }

    //TODO 全部关闭打开 HomeActivity
    public static void Out_Activity(Context context) {
        if (Integer.valueOf(SpUtils.get("id", 0).toString()) > 0) {
            for (Activity activity : FrameApplication.getActivitys()) {
                activity.finish();
            }
//            context.startActivity(new Intent(context, HomeActivity.class));
        }
    }

    //TODO 全部关闭打开登录
    public static void Out_Log(Context context) {
        if (Boolean.valueOf(SpUtils.get("Introduce", true).toString())) {
            for (Activity activity : FrameApplication.getActivitys()) {
                activity.finish();
            }
//            context.startActivity(new Intent(context, LoginActivity.class));
        }
    }

    /**
     * 设置Layout
     */
    protected abstract int getLayoutId();

    /**
     * 创建 presenter
     *
     * @return
     */
    protected abstract P loadPresenter();


    //-------------------------------------------

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化事件
     */
    protected abstract void initListener();

    /**
     * 加载数据
     */
    protected abstract void initData();

    /**
     * 设置事件
     */
    protected abstract void otherViewClick(View view);

    //-------------------------------------------

    /**
     * @return 显示的内容
     */
    public View getView() {
        view = View.inflate(this, getLayoutId(), null);
        return view;
    }

    /**
     * 点击的事件的统一的处理
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case 0:
                break;
            default:
                otherViewClick(view);
                break;
        }
    }

    /**
     * @param str 显示一个内容为str的toast
     */
    public void toast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    /**
     * @param contentId 显示一个内容为contentId指定的toast
     */
    public void toast(int contentId) {
        Toast.makeText(this, contentId, Toast.LENGTH_SHORT).show();
    }


    /**
     * @param str 日志的处理
     */
    public void LogE(String str) {
//        LogUtils.e(getClass(), str);
    }

    private void initCommonData() {
        // 关联view
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    /**
     * 解除关联
     */
    @Override
    protected void onDestroy() {
        // 解除关联
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        super.onDestroy();
        FrameApplication.getActivitys().remove(this);
    }

}